//
//  XUIAlertView.m
//  EtioniPad
//
//  Created by wu jingxing on 12-5-22.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "XUIAlertView.h"

@interface XUIAlertView ()
{
    void (^_clickBlock)(NSInteger nClickindex);
    
    XMSWaitSync *_waitSync;
}
@end

@implementation XUIAlertView

- (void)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)arOtherbuttontitles
{
    UIAlertView *view = [[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil] autorelease_mm];

    for (NSString *s in arOtherbuttontitles)
    {
        [view addButtonWithTitle:s];
    }
    
    [view setAssociatedRetainNonatomicValue:self withKey:"XUIAlertView"];

    [view show];
}

- (id)initWithSuspendThreadAlertView:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)arOtherbuttontitles clickBlock:(void (^)(NSInteger nClickindex))clickBlock
{
    id this = [super init];

    BOOL bMainthread = [NSThread isMainThread];

    if (bMainthread == NO)
    {
        _clickBlock = Block_copy(clickBlock);
        
        _waitSync = [XMSWaitSync new];

        dispatch_async(dispatch_get_main_queue(), ^
        {
            [self initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:arOtherbuttontitles];
        });

        [_waitSync wait];
    }
    else
    {
        [this autorelease_mm];
        this = nil;
    }

    return this;
}

- (id)initWithAlertView:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)arOtherbuttontitles clickBlock:(void (^)(NSInteger nClickindex))clickBlock
{
    self = [super init];

    _clickBlock = Block_copy(clickBlock);
    
    [self initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:arOtherbuttontitles];

    return self;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(_clickBlock != NULL)
        _clickBlock(buttonIndex);
    
    if(_waitSync != nil)
        [_waitSync stop];
}

- (void)dealloc
{
    if(_clickBlock != NULL)
        Block_release(_clickBlock);
    [_waitSync release_mm];
    superdealloc_mm;
}

+ (void)suspendThreadAlertViewWithTitle:(NSString *)szTitle message:(NSString *)szMessage cancelButtonTitle:(NSString *)szCancelbtntitle otherButtonTitles:(NSArray *)arOtherbuttontitles clickBlock:(void (^)(NSInteger nClickindex))clickBlock
{
    [[[XUIAlertView alloc] initWithSuspendThreadAlertView:szTitle message:szMessage cancelButtonTitle:szCancelbtntitle otherButtonTitles:arOtherbuttontitles clickBlock:clickBlock] autorelease_mm];
}

+ (void)alertViewWithTitle:(NSString *)szTitle message:(NSString *)szMessage cancelButtonTitle:(NSString *)szCancelbtntitle otherButtonTitles:(NSArray *)arOtherbuttontitles clickBlock:(void (^)(NSInteger nClickindex))clickBlock
{
    [[[XUIAlertView alloc] initWithAlertView:szTitle message:szMessage cancelButtonTitle:szCancelbtntitle otherButtonTitles:arOtherbuttontitles clickBlock:clickBlock] autorelease_mm];
}

@end


